package com.lei.dao.impl;

import com.lei.dao.BookDao;
import com.lei.entity.Book;
import com.lei.util.BaseDb;
import com.lei.util.TransBean;

import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/21 18:22
 */
public class BookDaoImpl implements BookDao {

    @Override
    public List<Book> getBookIdAndNameByTypeId(int typeid) {
        String sql = "select * from t_book where typeid = ? and status = 1 ";
        List<Map> maps = BaseDb.query(sql, typeid);
        return TransBean.populate(Book.class, maps);
    }

    @Override
    public Book getBookCountAndPriceById(String bookid) {
        String sql = "select * from t_book where bookid = ?";
        List<Map> maps = BaseDb.query(sql, bookid);
        if (maps.size() > 0) {
            return (Book) TransBean.populate(Book.class, maps).get(0);
        }
        return null;
    }

    @Override
    public long getCount(Book book) {
        StringBuilder sql = new StringBuilder("select count(*) c from t_book b join t_type t on  b.typeid = t.typeid where 1=1 ");
        if (book.getBookname() != null) {
            sql.append(" and  b.bookname like '%").append(book.getBookname()).append("%' ");
        }
        if (book.getAuthor() != null) {
            sql.append(" and b.getAuthor like '%").append(book.getAuthor()).append("%'");
        }
        if (book.getTypeid() != null) {
            sql.append(" and b.typeid = ").append(book.getTypeid());
        }
        List<Map> maps = BaseDb.query(sql + "");
        if (maps.size() > 0) {
            return (long) maps.get(0).get("c");
        }
        return 0;
    }

    @Override
    public List<Book> getAllBook(Book book) {
        StringBuilder sql = new StringBuilder("select * from t_book b join t_type t on b.typeid = t.typeid where 1=1 ");
        if (book.getBookname() != null) {
            sql.append(" and  b.bookname like '%").append(book.getBookname()).append("%' ");
        }
        if (book.getAuthor() != null) {
            sql.append(" and b.getAuthor like '%").append(book.getAuthor()).append("%'");
        }
        if (book.getTypeid() != null) {
            sql.append(" and b.typeid = ").append(book.getTypeid());
        }
        sql.append(" limit ?,? ");
        List<Map> maps = BaseDb.query(sql + "", (book.getPage() - 1) * book.getLimit(), book.getLimit());
        return TransBean.populate(Book.class, maps);
    }

    @Override
    public int updateStatus(Book book) {
        String sql = "update t_book set status = ? where bookid = ?";
        if (book.getStatus() == 0) {
            return BaseDb.update(sql, 1, book.getBookid());
        }
        return BaseDb.update(sql, 0, book.getBookid());
    }

    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book (bookname,author,count,remark,typeid,publisher,price,status) values (?,?,?,?,?,?,?,?)";
        return BaseDb.update(sql, book.getBookname(), book.getAuthor(), 0, book.getRemark(), book.getTypeid(), book.getPublisher(), book.getPrice(), 1);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update t_book set bookname = ?,author=?,remark=?,typeid=?,publisher=?,price=? where bookid = ?";
        return BaseDb.update(sql, book.getBookname(), book.getAuthor(), book.getRemark(), book.getTypeid(), book.getPublisher(), book.getPrice(), book.getBookid());
    }

    @Override
    public boolean checkBook(Book book) {
        String sql = "select bookid from t_book where bookname=? and author=? and publisher = ?";
        List<Map> maps = BaseDb.query(sql, book.getBookname(), book.getAuthor(), book.getPublisher());
        if (maps.size() > 0) {
            Integer resultId = Integer.parseInt(maps.get(0).get("bookid") + "");
            if (!resultId.equals(book.getBookid())) {
                // 找到了，并且Id不一样  不可用
                return false;
            }
        }
        return true;
    }

}
